import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'
import cart from '@/components/cart'
import item from '@/components/item'
import detail from '@/components/detail'
import mytest from '@/components/mytest'
import test from '@/components/test'
import register from '@/components/register'
import logn_in from '@/components/logn_in'
import weibo from '@/components/weibo'
import myprofile from '@/components/myprofile'
import goodscreate from '@/components/goodscreate'
import goodsshow from '@/components/goodsshow'
import editgood from '@/components/editgood'
// 导入注册页面
import reg from '@/components/reg'
// 导入登录页面
import login from '@/components/login'

//考试部分
import homework from '@/components/homework'

import kaoshi from '@/components/kaoshi'




import addgoods from '@/components/addgoods'
import goodslist from '@/components/goodslist'
import search from '@/components/search'

Vue.use(Router)

var routes = [
    // 添加拦截器
    {
        path: '/myprofile',
        name: 'myprofile',
        component: myprofile,
        beforeEnter: (to, from, next) => {
            //判断是否登录
            if (localStorage.getItem('username')) {
                console.log('已登录');
                next();
            } else {
                console.log('未登录');
                // 使用next进行重新定向到登陆页面
                next('/lognin');
            }
        }
    },

    {
        path: '/',
        name: 'index',
        component: index
    },
    {
        path: '/cart',
        name: 'cart',
        component: cart
    },
    {
        path: '/item',
        name: 'item',
        component: item
    },
    {
        path: '/detail',
        name: 'detail',
        component: detail
    },
    {
        path: '/mytest',
        name: 'mytest',
        component: mytest
    },
    {
        path: '/test',
        name: 'test',
        component: test
    },
    {
        path: '/register',
        name: 'register',
        component: register
    },
    {
        path: '/lognin',
        name: 'lognin',
        component: logn_in
    },
    {
        path: '/weibo',
        name: 'weibo',
        component: weibo
    },
    {
        path: '/goodscreate',
        name: 'goodscreate',
        component: goodscreate,
        beforeEnter: (to, from, next) => {
            //判断是否登录
            if (localStorage.getItem('admin') == 2) {
                console.log('管理员身份确认');
                next();
            } else {
                console.log('无权限操作');
                // 使用next进行重新定向到登陆页面
                next('/lognin');
            }
        }
    },
    {
        path: '/goodsshow',
        name: 'goodsshow',
        component: goodsshow,
        // beforeEnter: (to, from, next) => {
        //     //判断是否登录
        //     if (localStorage.getItem('admin') == 2) {
        //         console.log('管理员身份确认');
        //         next();
        //     } else {
        //         console.log('无权限操作');
        //         // 使用next进行重新定向到登陆页面
        //         next('/lognin');
        //     }
        // }
    },
    {
        path: '/reg',
        name: 'reg',
        component: reg
    },
    {
        path: '/login',
        name: 'login',
        component: login
    },
    // 3/20考试
    {
        path: '/homework',
        name: 'homework',
        component: homework
    },

    {
        path: '/editgood',
        name: 'editgood',
        component: editgood
    },
    // 添加商品
    {
        path: '/addgoods',
        name: 'addgoods',
        component: addgoods
    },
    // 展示商品列表
    {
        path: '/goodslist',
        name: 'goodslist',
        component: goodslist
    },
    // 4/3考试
    {
        path: '/kaoshi',
        name: 'kaoshi',
        component: kaoshi
    },
    // 搜索页面
    {
        path: '/search',
        name: 'search',
        component: search
    },
]

export default new Router({
    routes: routes,
    mode: 'history' /*hash*/
})